﻿using System.Collections;
using System.Data;
using System.Data.SqlClient;
using JJY.DBUtility;
using System.Collections.Generic;
using System;
using Odds.Utility;

namespace Odds.DBUtility
{
    public class OddsSQLHelper : SQLHelper
    {
        public static int ExecuteNonQuery(CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            //for second database
            //using (SqlConnection conn = new SqlConnection(OddsApplicationVariable.ConnectionStringProduction))
            //{
            //    ExecuteNonQuery(conn, cmdType, cmdText, commandParameters);
            //}
            //end

            using (SqlConnection conn = new SqlConnection(AppSettingManager.OddsConnectionString))
            {
                return ExecuteNonQuery(conn, cmdType, cmdText, commandParameters);
            }
        }

        public static SqlDataReader ExecuteReader(CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            //for second database
            //ExecuteReader(OddsApplicationVariable.ConnectionStringProduction,
            //       CommandType.Text, cmdText, commandParameters);
            //end

            return ExecuteReader(AppSettingManager.OddsConnectionString, CommandType.Text, cmdText, commandParameters);
        }

        /// <summary>
        /// please select dictionary's key as first column, and value as second column
        /// </summary>
        /// <param name="dr"></param>
        /// <returns></returns>
        public static Dictionary<string, Guid> GetStringKeyDictionary(string sql)
        {
            Dictionary<string, Guid> result = new Dictionary<string, Guid>();

            //for second database
            //SQLHelper.ExecuteReader(OddsApplicationVariable.ConnectionStringProduction, CommandType.Text, sql);
            //end

            using (SqlDataReader dr = SQLHelper.ExecuteReader(AppSettingManager.OddsConnectionString, CommandType.Text, sql))
            {
                while (dr.Read())
                {
                    result.Add(dr[0].ToString(), dr.GetGuid(1));
                }
            }
            return result;
        }

        /// <summary>
        /// please select dictionary's key as first column, and value as second column
        /// </summary>
        /// <param name="dr"></param>
        /// <returns></returns>
        public static Dictionary<int, Guid> GetIntKeyDictionary(string sql)
        {
            Dictionary<int, Guid> result = new Dictionary<int, Guid>();

            //for second database
            //SQLHelper.ExecuteReader(OddsApplicationVariable.ConnectionStringProduction, CommandType.Text, sql);
            //end

            using (SqlDataReader dr = SQLHelper.ExecuteReader(AppSettingManager.OddsConnectionString, CommandType.Text, sql))
            {
                while (dr.Read())
                {
                    result.Add(dr.GetByte(0), dr.GetGuid(1));
                }
            }
            return result;
        }

        public static DataTable GetDataTable(CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            return GetDataTable(AppSettingManager.OddsConnectionString, cmdType, cmdText, commandParameters);
        }

        public static object ExecuteScalar(string comText, params SqlParameter[] commandParameters)
        {
            return SQLHelper.ExecuteScalar(AppSettingManager.OddsConnectionString, CommandType.Text, comText, commandParameters);
        }
    }
}
